﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ultima;

namespace SpellCaster3.Forms
{
    public partial class ChooseUoDirectoryForm : Form
    {
        public ChooseUoDirectoryForm()
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;

            customUoDirectoryTextBox.Text = SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory;
            if (!string.IsNullOrWhiteSpace(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory))
                customUoDirectoryFolderBrowserDialog.SelectedPath = SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory;
        }

        public string Message { get { return messageLabel.Text; } set { if (value != null) messageLabel.Text = value; } }

        public string Directory { get { return customUoDirectoryTextBox.Text; } }

        public static Tuple<DialogResult, string> Show(string message = null, IWin32Window parent = null)
        {
            ChooseUoDirectoryForm frm = new ChooseUoDirectoryForm();
            DialogResult tmpResult = DialogResult.Cancel;
            frm.Message = message;
            if (parent != null)
            {
                frm.StartPosition = FormStartPosition.CenterParent;
                tmpResult = frm.ShowDialog(parent);
            }
            else
            {
                tmpResult = frm.ShowDialog();
            }
            return new Tuple<DialogResult, string>(tmpResult, frm.Directory);
        }

        private void customUoDirectoryButton_Click(object sender, EventArgs e)
        {
            if (customUoDirectoryFolderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                customUoDirectoryTextBox.Text = customUoDirectoryFolderBrowserDialog.SelectedPath;
                customUoDirectoryTextBox_Validating(sender, new CancelEventArgs(false));
            }
        }

        private void customUoDirectoryTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(customUoDirectoryTextBox.Text)) return;

            string path = customUoDirectoryTextBox.Text;

            e.Cancel = !Client.IsValidUltimaOnlineDirectory(path);
            if (e.Cancel)
            {
                customUoDirectoryTextBox.Text = "";
                ErrorForm.Show("Percorso non valido, la directory deve contenere un file client.exe o Client.exe e deve esistere", window: this);
            }
        }

        private void SaveConfig()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory))
                    Client.SetUltimaOnlineDirectory(SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Attenzione, il percorso specificato non è una directory valida di Ultima Online, valore resettato", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                customUoDirectoryTextBox.Text = "";
                SpellCaster3.Properties.Settings.Default.CustomUltimaOnlineDirectory = customUoDirectoryTextBox.Text;
                SpellCaster3.Properties.Settings.Default.Save();
            }
        }
        private void customUoDirectoryTextBox_TextChanged(object sender, EventArgs e)
        {
            customUoDirectoryFolderBrowserDialog.SelectedPath = customUoDirectoryTextBox.Text;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            SaveConfig();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
